import { isArray, isBoolean, isNumber, isObject, isString } from 'lodash-es'
import { sample } from 'openapi-sampler'
import {
    cloneDeepWith,
    deepUniqByValue,
    endsWithAny,
    equalsAny,
    includesAny,
} from '@/utils/jsonUtils'

// 将json转换为mock, 唱吧特殊逻辑
export const jsonToMockCloneCb = (
    key: string,
    value: any,
    keyPath?: string[],
) => {
    // 已经被mock过就不再mock
    if (
        key.includes('|') ||
        (typeof value === 'string' && value.includes('@'))
    ) {
        return { newKey: key, newValue: value, nextable: false }
    }

    // 时间
    if (
        endsWithAny(key, [
            'Date',
            'At',
            'Time',
            '-time',
            '-date',
            '_time',
            '_date',
        ]) ||
        equalsAny(key, ['date', 'at', 'time'])
    ) {
        const date = new Date()
        const year = date.getFullYear().toString().padStart(4, '0')
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        return {
            newKey: key,
            newValue: `@datetime(${year}-${month}-dd HH:mm:ss)`,
        }
    }

    // id
    if (
        endsWithAny(key, ['Id', 'ID', 'Key']) ||
        equalsAny(key, ['id', 'key'])
    ) {
        return { newKey: key, newValue: '@id()' }
    }

    // 图片
    if (
        equalsAny(key, [
            'img',
            'image',
            'Img',
            'Image',
            'Icon',
            'icon',
            'IconUrl',
            'iconUrl',
            'avatar',
            'Avatar',
            'AvatarUrl',
            'avatarUrl',
            'headphoto',
        ])
    ) {
        return { newKey: key, newValue: '@image()' }
    }

    // 是不是礼物项
    if (
        (keyPath?.includes('prizes') || keyPath?.includes('prize')) &&
        isObject(value) &&
        !Array.isArray(value)
    ) {
        return {
            newKey: key,
            newValue: {
                id: '@string(number, 6)',
                type: '@prizetype()',
                name: '@ctitle()',
                expire: '@integer(0, 30)',
                num: '@integer(1, 30)',
                coins: '@integer(0, 999999)',
                img: '@image()',
                metadata: {
                    isDynamic: '@pick(true, false)',
                    isSVip: '@pick(true, false)',
                    isVip: '@pick(true, false)',
                    commodityLevel: '@commoditylevel()',
                },
            },
            nextable: false,
        }
    }

    // 原始值
    if (isNumber(value)) {
        return { newKey: key, newValue: '@integer(1, 1000)' }
    }

    if (isString(value)) {
        if (endsWithAny(key, ['Num', 'Count', 'Score', 'score'])) {
            return { newKey: key, newValue: '@string(number, 0, 3)' }
        }
        if (includesAny(key, ['Description', 'description'])) {
            return { newKey: key, newValue: '@csentence()' }
        }
        if (key === 'url') {
            return { newKey: key, newValue: '@url()' }
        }
        if (key === 'email') {
            return { newKey: key, newValue: '@email()' }
        }
        return { newKey: key, newValue: '@ctitle()' }
    }

    if (isBoolean(value)) {
        return { newKey: key, newValue: '@boolean()' }
    }

    if (isArray(value)) {
        return { newKey: `${key}|0-20`, newValue: value }
    }

    return { newKey: key, newValue: value }
}

// 将json转换为MockJS模板
export const jsonToMock = (json?: any) => {
    if (!json) return {}
    const data = cloneDeepWith(json, jsonToMockCloneCb)
    const uniqued = deepUniqByValue(data)
    return uniqued
}

// 将jsonSchema转换为MockJS模板
export const jsonSchemaToMock = (jsonSchema?: any) => {
    if (!jsonSchema) return {}
    const jsonValue = sample(jsonSchema)
    const data = jsonToMock(jsonValue)

    // 唱吧特殊逻辑
    let result = data

    if (!result?.hasOwnProperty('code')) {
        result = {
            code: 0,
            data: data,
            msg: '@csentence()',
        }
    }

    return result
}
